<?php
namespace classlib\core\view;
class xml extends view_abstract {
	private $smart = array();
	public function __construct($params=null) {
		$this->smart = array('code'=>0);
	}
	
	/**
	 * 变量赋值
	 * @param string $key
	 * @param mix    $val
	 * @return view_abstract
	 */
	public function assign($key, $val) {
		$this->smart[$key] = $val;
		return $this;
	}
	
	/**
	 * 变量赋值引用赋值
	 * @param string $key
	 * @param mix $val
	 * @return view_abstract
	 */
	public function assign_by_ref($key, &$val) {
		$this->smart[$key] = $val;
		return $this;
	}

	/**
	 * 已经分配的变量的获取 默认取所有
	 * @param string $key
	 * @return mix
	 */
	public function get_template_vars($key=false) {
		if($key) {
			$data = isset($this->smart[$key])? $this->smart[$key]:false;
			return $data;
		}
		return $this->smart;
	}
	
	/**
	 * 将要输出的资料获取
	 * @param string  $tpl
	 * @param boolean $iscache
	 * @return string
	 */
	public function fetch($tpl=false) {
		if ($this->smart['code'] == 0) {
			unset($this->smart['msg']);
		}
		$str = $this->array2xml($this->smart);
		return $str;
	}
	
	/**
	 * 将数据转成xml格式化输入
	 * Enter description here ...
	 * @param unknown_type $arr
	 * @param unknown_type $level
	 */
	public function array2xml($arr, $level = 1) {
        $str = ($level == 1)? "<root>":'';
        foreach($arr as $key => $val) {
        	if (is_numeric($key)) {
            	$key = 'item';
            }
            if(!is_array($val)) {
            	if (is_string($val) && preg_match('/[&<>"\'\?]+/', $val)) {
            		$str .= "<$key><![CDATA[".$val.']]>'."</$key>";
            	} else {
            		$str .= "<$key>".$val."</$key>";
            	}
            } else {
                $str .= "<$key>".$this->array2xml($val, $level+1)."</$key>";
            }
        }
        if($level == 1) {
          	$str .= '</root>';
        }
        return $str;
    }
    
    /**
     * 将数据转成xml格式化输入
     * Enter description here ...
     * @param unknown_type $arr
     * @param unknown_type $level
     */
    public function beautify($arr, $level = 1) {
    	$str = ($level == 1)? "<root>\r\n" : '';
    	$space = str_repeat("\t", $level);
    	foreach($arr as $key => $val) {
    		if (is_numeric($key)) {
    			$key = 'item';
    		}
    		if(!is_array($val)) {
    			if (is_string($val) && preg_match('/[&<>"\'\?]+/', $val)) {
    				$str .= $space."<$key><![CDATA[".$val.']]>'."</$key>\r\n";
    			} else {
    				$str .= $space."<$key>".$val."</$key>\r\n";
    			}
    		} else {
    			$str .= $space."<$key>\r\n".$this->array2xml($val, $level+1).$space."</$key>\r\n";
    		}
    	}
    	if($level == 1) {
    		$str .= '</root>';
    	}
    	return $str;
    }
    
    /**
     * 出错的时候输出信息
     * {@inheritDoc}
     * @see \classlib\core\view\view_abstract::assign_error()
     */
    public function assign_error($code, $msg) {
    	$this->smart		 = array();
		$this->smart['code'] = $code;
		$this->smart['msg']  = $msg;
		$this->display();
		exit;
    }
    
    /**
	 * 重载Smarty输出的资料输出
	 * @param string  $tpl
	 * @param int     $caching 
	 * @param string  $cacheid  区别同一个页面不同展示模版
	 * @param int     $lifetime
	 * @see Smarty  $caching
	 */
	public function display($tpl=false, $caching=0, $cacheid=null, $lifetime=3600) {
		parent::display();
		echo $this->fetch();
	}
}